package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.devices;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

public class DeviceBean {

	private Device device;
	
	private Boolean selected;
	
	private SystemService service;
	
	public DeviceBean(Device dev, SystemService serv) {
		setService(serv);
		device = dev;
	}

	public SystemService getService() {
		return service;
	}

	public void setService(SystemService service) {
		this.service = service;
	}
	
	public String getTypename() {
		return device.getType().getName();
	}

	public Boolean getSelected() {
		return selected;
	}

	public void setSelected(Boolean selected) {
		this.selected = selected;
	}

	public String getName() {
		return device.getName();
	}

	public void setName(String name) {
		device.setName(name);
	}

	public String getDescription() {
		return device.getDescription();
	}

	public void setDescription(String description) {
		device.setDescription(description);;
	}

	public Integer getSoundingTime() {
		return device.getSoundingTime();
	}

	public void setSoundingTime(Integer soundingTime) {
		device.setSoundingTime(soundingTime);
	}

	public Boolean getVisible() {
		return device.getVisible();
	}

	public void setVisible(Boolean visible) {
		device.setVisible(visible);
	}

	public String getIP() {
		return device.getIP();
	}

	public void setIP(String iP) {
		device.setIP(iP);
	}

	public Integer getPort() {
		return device.getPort();
	}

	public void setPort(Integer port) {
		device.setPort(port);
	}

	public Integer getUnitID() {
		return device.getUnitID();
	}

	public void setUnitID(Integer unitID) {
		device.setUnitID(unitID);
	}

	public Boolean getAutoConnection() {
		return device.getAutoConection(); 
	}

	public void setAutoConnection(Boolean autoConnection) {
		device.setAutoConection(autoConnection);
	}

	public void edit(){
		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selected", this);
	}
	
	public void delete(){
		service.deleteDevice(device);
	}

	public void saveChanges(){
		service.saveDevice(device);
	}
	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public static List<DeviceBean> wrap(List<Device> devices, SystemService serv) {
		List<DeviceBean> ret = new ArrayList<DeviceBean>();

		for (Device dev : devices) {
			ret.add(new DeviceBean(dev,serv));
		}
		return ret;
	}
}
